package com.company.project.common.job.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.entity.*;
import com.company.project.mapper.*;
import com.company.project.service.TSeatsService;
import com.company.project.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 测试定时任务(演示Demo，可删除)
 * testTask为spring bean的名称， 方法名称必须是run
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */
@Component("middlenumTask")
public class MiddlenumTask {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private TSeatsService tSeatsService;
	@Resource
	public TSeatsMapper tSeatsMapper;
	@Resource
	private TMiddlenumMapper tMiddlenumMapper;

	@Resource
	private TXiaohaoMapper tXiaohaoMapper;
	@Resource
	private THwtjMapper hwtjMapper;
	@Resource
	private TWdkhMapper wdkhMapper;
	@Resource
	private UserService userService;
	@Resource
	private TGzszMapper gzszMapper;
	@Resource
	private TGhszMapper tgHszMapper;
	@Resource
	private TGhszMapper tGhszMapper;
	@Resource
	private TGhkhMapper ghkhMapper;
	@Resource
	private TSeatsMapper seatsMapper;
	@Resource
	private TThrbMapper tThrbMapper;


	public void run(String params){
		logger.debug("MiddlenumTask定时任务正在执行，参数为：{}", params);
		//刷新中间号分数
		List<TMiddlenumEntity> tMiddlenumEntities = tMiddlenumMapper.selectList(Wrappers.<TMiddlenumEntity>lambdaQuery());
		for (TMiddlenumEntity t : tMiddlenumEntities) {
			t.setHjcs(0);
			t.setSycs(t.getMtczcs());
			tMiddlenumMapper.updateById(t);
			//小号重置分钟数
			List<TXiaohaoEntity> tXiaohaoEntities = tXiaohaoMapper.selectList(Wrappers.<TXiaohaoEntity>lambdaQuery().eq(TXiaohaoEntity::getXiaohao, t.getMiddlenum()));
			for (TXiaohaoEntity tXiaohao : tXiaohaoEntities) {
				tXiaohao.setShengyucishu(t.getMtczcs());
				tXiaohaoMapper.updateById(tXiaohao);
			}
		}
		logger.debug("MiddlenumTask定时任务正在执行，参数为：{}", params);

		//清空删除的公司
		hwtjMapper.deleteByCompany();

//		//每天更新话务统计
//		List<THwtjEntity> hwtjs = hwtjMapper.selectALL();
//		for (THwtjEntity t : hwtjs) {
//			THwtjEntity hwtj = hwtjMapper.selectOneByCorporatename(t.getCompany());
//			hwtj.setId("");
//			hwtj.setBscs("0");
//			hwtj.setJts("0");
//			hwtj.setJfsc("0");//计费时长
//			hwtj.setThsc("0");//通话时长
//			hwtj.setDate(new Date());
//			hwtjMapper.insert(hwtj);
//		}
		//流入公海
		rlghTask();
		//通话统计
		thtjTask();
	}

	private void thtjTask() {
		Date date = new Date();
		LocalTime now = LocalTime.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-DD");
		String currentTime = now.format(formatter);

		tThrbMapper.insertThjl(currentTime,"天","ydhxtr","");
		//代理商
		List<SysUser> sysUsers = tThrbMapper.selectDls();
		for (SysUser user: sysUsers) {
			tThrbMapper.insertThjl(currentTime,"天",user.getUsername(),user.getUsername());
		}
	}

	private void rlghTask() {
		//公司名称
		QueryWrapper<TSeatsEntity> distinctCor = Wrappers.<TSeatsEntity>query().select("distinct corporatename");
		List<String> objects = tSeatsService.listObjs(distinctCor);
		for (String cor : objects) {
			//根据公司名称查询规则
			TGzszEntity tGzszEntity = gzszMapper.selectOne(Wrappers.<TGzszEntity>lambdaQuery().eq(TGzszEntity::getSsgs, cor));
			Integer wcjhl = tGzszEntity.getWcjhl();//未成交回流
			Integer wgjhl = tGzszEntity.getWgjhl();//未跟进回流
			Date lrgh2 = new Date();
			//根据公司名称查询公海
			List<TGhszEntity> tGhszEntities = tgHszMapper.selectList(Wrappers.<TGhszEntity>lambdaQuery().eq(TGhszEntity::getSsgs, cor));
			for (TGhszEntity ghsz : tGhszEntities) {
				List<TWdkhEntity> list = wdkhMapper.selectList(Wrappers.<TWdkhEntity>lambdaQuery().eq(TWdkhEntity::getSsgh, ghsz.getGhmc()).eq(TWdkhEntity::getIscj,"0"));
				for (TWdkhEntity wdkh: list) {
					Date zhgjtime = wdkh.getZhgjtime();//最后跟进时间
					Date rktime = wdkh.getRktime();//入库时间
					//设置流入公海时间  未成交回流   入库时间加上设置时间
					Calendar calendar2 = Calendar.getInstance();
					calendar2.setTime(rktime);
					//加上规定时间
					calendar2.add(Calendar.DAY_OF_MONTH, wcjhl);
					lrgh2 = calendar2.getTime();
					Date lrgh = new Date();

					//设置流入公海时间  未跟进回流
					Calendar calendar = Calendar.getInstance();
					calendar.setTime(zhgjtime);
					//加上规定时间
					calendar.add(Calendar.DAY_OF_MONTH, wgjhl);
					lrgh = calendar.getTime();
					Date hlDay = lrgh.compareTo(lrgh2) > 0 ? lrgh2 : lrgh;
					wdkh.setLrghtime(hlDay);

					wdkhMapper.updateById(wdkh);
				}
			}
		}
		SysUser userInfo = userService.getUserInfo();
		TSeatsEntity tSeatsEntity = seatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery()
				.eq(TSeatsEntity::getAccountid, userInfo.getId()));
		//流入公海
		List<String> ids = wdkhMapper.selectLrgh();
		for (String id : ids) {
			TWdkhEntity wdkh = wdkhMapper.selectById(id);
			TGhkhEntity ghkh = new TGhkhEntity();
			ghkh.setKhname(wdkh.getKhname());
			ghkh.setLxr(wdkh.getLxr());
			ghkh.setLxdh(wdkh.getLxdh());
			ghkh.setSsxs("");
			ghkh.setKhjb(wdkh.getKhjb());
			ghkh.setQdcs(wdkh.getQdcs());
			ghkh.setGjjl(wdkh.getGjjl());
			ghkh.setZhgjtime(wdkh.getZhgjtime());
			ghkh.setRktime(new Date());
			ghkh.setKhly(wdkh.getKhly());
			ghkh.setRghyy("自动流入");
			ghkh.setSsgh(wdkh.getSsgh());
			ghkh.setWdkhid(id);
			ghkhMapper.insert(ghkh);
			TGhszEntity tGhszEntity = tGhszMapper.selectList(Wrappers.<TGhszEntity>lambdaQuery().eq(TGhszEntity::getGhmc, wdkh.getSsgh()).eq(TGhszEntity::getSsgs, tSeatsEntity.getCorporatename())).get(0);
			//变更客户数量
			tGhszEntity.setKhsl(tGhszEntity.getKhsl() + 1);
			tGhszMapper.updateById(tGhszEntity);
		}

		wdkhMapper.rlgh(ids);

	}
}
